package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.ScenicComment;
import com.wzk.travelspringboot.service.ScenicCommentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/scenicComment")
@RequiredArgsConstructor
@Validated
public class AdminScenicCommentController {

    private final ScenicCommentService scenicCommentService;

    /**
     * 分页查询所有景区评论
     */
    @GetMapping("/list")
    public Result listScenicComments(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ScenicComment> page = new Page<>(pageNum, pageSize);
        Page<ScenicComment> commentPage = scenicCommentService.page(page);
        return Result.success(commentPage);
    }

    /**
     * 根据景区ID查询评论
     */
    @GetMapping("/scenic/{scenicId}")
    public Result getCommentsByScenicId(
            @PathVariable("scenicId") @NotNull(message = "景区ID不能为空") Long scenicId) {
        // 使用已有的服务方法
        return scenicCommentService.allCommentById(String.valueOf(scenicId));
    }

    /**
     * 根据用户ID查询评论
     */
    @GetMapping("/user/{userId}")
    public Result getCommentsByUserId(
            @PathVariable("userId") @NotNull(message = "用户ID不能为空") Long userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ScenicComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ScenicComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ScenicComment::getUserId, userId);
        Page<ScenicComment> commentPage = scenicCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }

    /**
     * 根据ID删除评论
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteScenicComment(@PathVariable("id") @NotNull(message = "评论ID不能为空") Long id) {
        boolean success = scenicCommentService.removeById(id);
        return success ? Result.success("删除评论成功") : Result.failed("删除评论失败");
    }

    /**
     * 根据内容模糊查询评论
     */
    @GetMapping("/search")
    public Result searchCommentByContent(
            @RequestParam String content,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ScenicComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ScenicComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ScenicComment::getContent, content);
        Page<ScenicComment> commentPage = scenicCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }

    /**
     * 根据点赞数排序查询评论
     */
    @GetMapping("/hot")
    public Result getHotComments(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<ScenicComment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ScenicComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(ScenicComment::getLike);
        Page<ScenicComment> commentPage = scenicCommentService.page(page, queryWrapper);
        return Result.success(commentPage);
    }
} 